package com.treehouse.gui.viewport;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import com.treehouse.gui.actorview.IActorViewController;
import com.treehouse.gui.consoleview.IConsoleViewController;
import com.treehouse.gui.loaderview.ILoaderViewController;
import com.treehouse.service.application.IApplicationService;

public class ViewPortController implements IViewPortController, ActionListener {

	private JFrame viewPort;
	private JPanel mainPanel;
	private JPanel topPanel;
	private IConsoleViewController consoleViewController;
	private IActorViewController actorViewController;
	private ILoaderViewController loaderViewController;
	private IApplicationService applicationService;

	public ViewPortController(IConsoleViewController console,
			IActorViewController actor, ILoaderViewController loader,
			IApplicationService service) {
		this.applicationService = service;
		this.initializeViewPort();
		this.consoleViewController = console;
		this.actorViewController = actor;
		this.loaderViewController = loader;
		this.doLayout();
	}

	private void initializeViewPort() {
		this.viewPort = new JFrame(this.applicationService.getApplicationName());
		this.viewPort.setSize(this.applicationService.getResolution());
		this.topPanel = new JPanel();
		this.mainPanel = new JPanel();
		this.addMenuBar();
		this.viewPort.addWindowListener(new WindowListener() {

			@Override
			public void windowActivated(WindowEvent arg0) {

			}

			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				applicationService.terminateApplication();
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {

			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {

			}

			@Override
			public void windowIconified(WindowEvent arg0) {

			}

			@Override
			public void windowOpened(WindowEvent arg0) {

			}

		});

	}
	
	private void addMenuBar(){
		JMenuBar menuBar = new JMenuBar();
		
		JMenu datei = new JMenu("Datei");
		menuBar.add(datei);
		JMenuItem beenden = new JMenuItem("Programm beenden");
		datei.add(beenden);
		beenden.addActionListener(this);
		this.viewPort.setJMenuBar(menuBar);
	}
	
	private void doLayout() {
		GridBagLayout gblTop = new GridBagLayout();
		GridBagConstraints gbcSmallT = new GridBagConstraints();
		GridBagConstraints gbcBigT = new GridBagConstraints();
		gbcBigT.gridx = 1;
		gbcBigT.gridy = 0;
		gbcSmallT.gridx = 0;
		gbcBigT.weightx = 0.8;
		gbcBigT.weighty = 0.8;
		gbcSmallT.weightx = 0.2;
		gbcSmallT.weighty = 0.2;
		gbcSmallT.gridy = 0;
		gbcBigT.fill = GridBagConstraints.BOTH;
		gbcSmallT.fill = GridBagConstraints.BOTH;
		
		this.topPanel.setLayout(gblTop);
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbcSmall = new GridBagConstraints();
		GridBagConstraints gbcBig = new GridBagConstraints();
		gbcBig.gridx = 0;
		gbcBig.gridy = 0;
		gbcSmall.gridx = 0;
		gbcBig.weightx = 0.8;
		gbcBig.weighty = 0.8;
		gbcSmall.weightx = 0.2;
		gbcSmall.weighty = 0.2;
		gbcSmall.gridy = 1;
		gbcBig.fill = GridBagConstraints.BOTH;
		gbcSmall.fill = GridBagConstraints.BOTH;
		
		gbl.addLayoutComponent(this.topPanel, gbcBig);
		gbl.addLayoutComponent(this.consoleViewController.getComponent(), gbcSmall);
		
		gblTop.addLayoutComponent(this.actorViewController.getComponent(), gbcSmallT);
		gblTop.addLayoutComponent(this.loaderViewController.getComponent(), gbcBigT);
		this.topPanel.setPreferredSize(new Dimension(1900, 400));
		this.mainPanel.setLayout(gbl);
		this.viewPort.add(this.mainPanel);
		this.mainPanel.add(this.topPanel);
		this.mainPanel.add(this.consoleViewController.getComponent());
		this.topPanel.add(this.actorViewController.getComponent());
		this.topPanel.add(this.loaderViewController.getComponent());

	}

	public void setVisible(boolean visible) {
		this.viewPort.setVisible(visible);
	}

	@Override
	public void setConsoleViewController(
			IConsoleViewController consoleViewController) {
		this.consoleViewController = consoleViewController;
		this.doLayout();
	}

	@Override
	public void setActorViewController(IActorViewController actorViewController) {
		this.actorViewController = actorViewController;
		this.doLayout();
	}

	@Override
	public void setLoaderViewController(
			ILoaderViewController loaderViewController) {
		this.loaderViewController = loaderViewController;
		this.doLayout();
	}

	@Override
	public void setIApplicationService(IApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();
		
		if(command.equals("Programm beenden")){
		this.applicationService.terminateApplication();
		}
		
		
	}

}
